package com.bharatonjava.swift.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bharatonjava.swift.dao.mappers.RoleMapper;
import com.bharatonjava.swift.domain.Role;

@Repository
public class RoleDao implements IRoleDao{
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public int saveRole(Role role) {
		String sql = "INSERT INTO ROLES(ROLENAME) VALUES(?)";
		int updates = this.jdbcTemplate.update(sql, new Object[]{role.getRoleName()});
		return updates;
	}

	@Override
	public List<Role> getRolesByUsername(
			String userName) {
		String sql = "SELECT ROLENAME FROM USERS_ROLES WHERE USERNAME = ?";
		List<Role> roles = (List<Role>) this.jdbcTemplate.query(sql,new Object[]{userName}, new RoleMapper());
		return roles;
	}

	@Override
	public int deleteRole(Role role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getAllRoles() {
		List<Role> roles = null;
		String sql = "SELECT ROLENAME FROM ROLES";
		roles = (List<Role>) this.jdbcTemplate.query(sql, new RoleMapper());
		return roles;
	}

}
